<template>
  <div class="ToolingStageLife">
    <el-row style="paddingBottom: 0">
      <el-col :span="24">
        <dynForm :info-form="infoForm" :search-tags="searchTags" :select-list="selectList">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t("search") }}</el-button
            >
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >{{ $t("add") }}</el-button
      >
      <el-button
        v-btnPermission:edit
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"
        @click="handleModify"
      >
        {{ $t("modify") }}</el-button
        >
      <el-button
        v-btnPermission:delete
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="handleDel"
      >{{ $t("delete") }}</el-button
      >
    </div>
    <!-- 表单部分 -->
    <el-card class="box-card" shadow="never">
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="getList"
      />
    </el-card>
    <lifeForm ref="lifeForm" @initList="initList"/>
  </div>
</template>

<script>
import networkTips from '@/mixins/networkTips'
import mySetting from '@/mixins/mySetting'
import stageLifeApi from '@/api/Tooling/toolingStageLife'
import lifeForm from './components/dialogForm'
export default {
  name: 'ToolingStageLife',
  components: { lifeForm },
  mixins: [mySetting, networkTips],
  props: {},
  data() {
    return {
      infoForm: {},
      searchTags: [
        { label: this.$t('custName'), prop: 'custName', tag: 'DynInput' },
        { label: this.$t('productName'), prop: 'productName', tag: 'DynInput' },
        { label: this.$t('lifeTime'), prop: 'lifeTime', tag: 'DynSelect' }
      ],
      selectList: {},
      tableData: [],
      tableLoading: true,
      tableColumns: [
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        { prop: 'custName', label: this.$t('custName') },
        { prop: 'productName', label: this.$t('productName') },
        { prop: 'lifeTime', label: this.$t('lifeTime') },
        { prop: 'creator', label: this.$t('creator') },
        { prop: 'createTime', label: this.$t('createTime') }
      ]
    }
  },
  computed: {},
  watch: {},
  created() {
    this.initList()
    this.getLifeTimeList()
  },
  mounted() {},
  methods: {
    // 初始化
    async initList() {
      const res = await stageLifeApi.getList(
        this.$clearNull({ ...this.infoForm, ...this.listQuery }))
      this.total = res.total
      this.tableData = res.data
      this.tableLoading = false
    },
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    handleCreate() {
      this.$refs.lifeForm.handleOpen()
    },
    handleModify() {
      this.$refs.lifeForm.handleOpen(this.multipleSelection[0].id)
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    },
    handleDel() {
      this.$confirm(
        this.$t('confirmDelete') +
        this.multipleSelection.length +
        this.$t('recordCount'),
        this.$t('tip'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).then(() => {
        const ids = this.multipleSelection.map((ele) => {
          return ele.id
        })
        stageLifeApi.doDelete({ ids }).then(res => {
          this.netTips(res, this.setOnePage, this.initList)
        })
      })
    },
    getList(val) {
      this.listQuery = val
      this.initList()
    },
    async getLifeTimeList() {
      const res = await stageLifeApi.getLifeTimeList()
      this.$set(this.selectList, 'lifeTime', res.data)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/defaultStyle.scss";
::v-deep .el-form{
    .el-form-item:last-child{
        margin:0 0 15px 0;
    }
}
</style>
